package myapp.Chat;

import java.util.List;

import myapp.DTO.UserDTO;
import myapp.googlemap.R;
import myapp.services.ChatServiceHelper;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class RequesterAdapter extends ArrayAdapter<UserDTO>{
	private int resource;
	private String userEmail;

	public RequesterAdapter(Context context, int textViewResourceId,
			List<UserDTO> objects, String userEmail) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
		this.resource = textViewResourceId;
		this.userEmail = userEmail;
	}
	
//	public UserDTOAdapter(Context context, int resource,
//			int textViewResourceId, List<UserDTO> objects) {
//		super(context, resource, textViewResourceId, objects);
//		// TODO Auto-generated constructor stub
//		this.resource = resource;
//	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Log.i("RequesterAdapter", "[getView]-start");
		LinearLayout userView;
		final UserDTO requester = getItem(position);
		
		if (convertView == null){
			userView = new LinearLayout(getContext());
			String inflater = Context.LAYOUT_INFLATER_SERVICE;
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(inflater);
			vi.inflate(resource, userView, true);
		}
		else{
			userView = (LinearLayout) convertView;
		}
		
		//Log.i("RequesterAdapter", "[getView]userView null? : " + (userView==null));
		
		//Get View of txt will contain data
		TextView requesterName = (TextView) userView.findViewById(R.id.tvRequesterName);
		TextView requesterEmail = (TextView) userView.findViewById(R.id.tvRequesterEmail);
		
		Button btnAccept =(Button) userView.findViewById(R.id.btnAcceptRequest);
		btnAccept.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Log.i("RequesterAdapter", "[btnAccept clicked] user: " + userEmail + " requester:" + requester.email);
				AcceptRequest(userEmail, requester.email);
				VisualizeAcceptButtonClicked(arg0);
			}
		});
		
		Button btnReject = (Button) userView.findViewById(R.id.btnRejectRequest);
		btnReject.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Log.i("RequesterAdapter", "[btnReject clicked] user: " + userEmail + " requester:" + requester.email);
				RejectRequest(userEmail, requester.email);
				VisualizeRejectButtonClicked(arg0);
			}
		});
		
		//Assign data to txt
		requesterName.setText(requester.name);
		requesterEmail.setText(requester.email);
		userView.setTag(requester);
		
		return userView;
	}
	
	private void AcceptRequest(String userEmail, String requesterEmail) {
		ChatServiceHelper helper = new ChatServiceHelper();
		helper.sendAcceptFriendRequest(userEmail, requesterEmail);
	}
	
	private void RejectRequest(String userEmail, String requesterEmail){
		ChatServiceHelper helper = new ChatServiceHelper();
		helper.sendFriendRequest(userEmail, requesterEmail);
	}
	
	private void VisualizeAcceptButtonClicked(View view) {
		try {
			Button btnClicked = (Button) view;
			btnClicked.setOnClickListener(null);
			View parent = (View) view.getParent();
			Button btnReject = (Button) parent.findViewById(R.id.btnRejectRequest);
			btnReject.setVisibility(View.INVISIBLE);	//Make btnReject disappear
		} catch (Exception e) {
			// TODO: handle exception
			Log.e("RequesterAdapter", "[ProcessAcceptButtonClicked] exception:" + e.getMessage() + e.getLocalizedMessage() + e.getCause());
		}
	}
	
	private void VisualizeRejectButtonClicked(View view){
		try {
			View listViewItem = (View) view.getParent().getParent();
			listViewItem.setVisibility(View.GONE);
		} catch (Exception e) {
			// TODO: handle exception
			Log.e("RequesterAdapter", "[ProcessRejectButtonClicked] exception:" + e.getMessage() + e.getLocalizedMessage() + e.getCause());
		}
	}
}
